package com.evil.DAO;

import com.evil.DB_Utils.*;
import com.evil.DB_Utils.ErrorInfo;
import com.evil.Data.Data;
import com.evil.Utils.PropertiesUtils;
import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.Date;

/********************************************
 * 工程名：Web
 * 包名：com.evil.DAO
 * 时间：16-9-12 下午7:32
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：积分操作DAO
 ********************************************/
public class IntegralDAO
{
    private static Logger log = Logger.getLogger(IntegralDAO.class);

    private static final int OLD_YEAR = 0;/*年 过期*/
    private static final int OLD_MOUTH = 1;/*月 过期*/
    private static final int OLD_DATE = 2;/*日 过期*/


    private int integral_month;
    private int integral_today;

    private String returnString;/*返回信息*/


    /**
     * 更新积分
     */
    public boolean updateIntegral(String phone, int sumFlow)
    {
        String url = "UPDATE `edasupport`.`user`\n" +
                "SET `integral` = integral +?\n" +
                "WHERE\n" +
                "	(`phone` = ?);";
        String re = DB_Utils
                .update(url).add(sumFlow).add(phone).build().close();
        return re.equals("1");
    }

    /**
     * 更新每日 每月积分
     */
    public boolean updateIntegral_month_today(String phone, int sumFlow)
    {
        /*更新积分->在原基础上相加*/
        String updateUrl = "UPDATE `edasupport`.`integral`\n" +
                "SET `integral_month` = integral_month +?," +
                " `integral_today` = integral_today +?\n" +
                "WHERE\n" +
                "(`phone` = ?);";

        String reInfo = DB_Utils.update(updateUrl)
                .add(sumFlow)
                .add(sumFlow)
                .add(phone).build().close();

        return reInfo.equals("1");
    }

    /**
     * 查询每日 每月积分
     * 如果不存在该条目，插入
     * 如果碰到每月 每日第一次查询，则将积分置0
     */
    public String findIntegral(String phone)
    {
        returnString = null;
        /*如果为空，插入*/
        String insertUrl = "INSERT INTO `edasupport`.`integral` (`phone`, `integral_month`, `integral_today`, `datetime`) VALUES (?, ?, ?, ?);";

        /*查询*/
        String queryUrl = "SELECT\n" +
                "integral.integral_month,\n" +
                "integral.integral_today,\n" +
                "integral.datetime\n" +
                "FROM\n" +
                "integral\n" +
                "WHERE\n" +
                "integral.phone = ?";

        DB_Utils.query(queryUrl)
                .add(phone)
                .build()
                .get(next ->
                {
                    if (next.next())
                    {
                        integral_month = next.getInt(1);
                        integral_today = next.getInt(2);

                        Date date = next.getDate(3);
                        Calendar calendar_old = Calendar.getInstance();
                        calendar_old.setTime(date);

                        Calendar calendar = Calendar.getInstance();

                        String BREAKSTRING = new PropertiesUtils().get("BREAKSTRING");

                        if (calendar.get(Calendar.YEAR) == calendar_old.get(Calendar.YEAR))
                            if (calendar.get(Calendar.MONTH) == calendar_old.get(Calendar.MONTH))
                                if (calendar.get(Calendar.DATE) == calendar_old.get(Calendar.DATE))
                                    returnString = integral_month + BREAKSTRING + integral_today;
                                else
                                {
                                    returnString = integral_month + BREAKSTRING + 0;
                                    update_OldIntegral_To_0(phone, OLD_DATE);
                                }
                            else
                                update_OldIntegral_To_0(phone, OLD_MOUTH);
                        else
                            update_OldIntegral_To_0(phone, OLD_YEAR);
                    }
                    /*如果为空则插入*/
                    else
                    {
                        String re = DB_Utils.insert(insertUrl)
                                .add(phone)
                                .add(0)
                                .add(0)
                                .add(new Date())
                                .build().close();
                        if (re.equals(ErrorInfo.ERROR_NO_FOREIGN_VALUE.toString()))
                            log.error(phone + "发生严重崩溃错误，位置:" + IntegralDAO.class.getName()+"\r\n");
                    }
                }).close();
        return returnString;
    }

    /**
     * 如果积分过期则为0
     */
    private void update_OldIntegral_To_0(String phone, int type)
    {
        /*如果过期，则为0*/
        String updateUrl = "UPDATE `edasupport`.`integral`\n" +
                "SET `integral_month` = ?,\n" +
                " `integral_today` = ?,\n" +
                " `datetime` = ?\n" +
                "WHERE\n" +
                "`phone` = ?;";

        Update update = DB_Utils.update(updateUrl).build();
        if (type == OLD_YEAR)
        {
            System.out.println("OLD_YEAR");
            update.add(0);
            update.add(0);
        }
        else if (type == OLD_MOUTH)
        {
            System.out.println("OLD_MOUTH");
            update.add(0);
            update.add(0);
        }
        else if (type == OLD_DATE)
        {
            System.out.println("OLD_DATE");
            update.add(integral_month);
            update.add(0);
        }

        update.add(new Date());
        update.add(phone);

        String re = update.build().close();
    }


}
